package com.example.fill;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Scroller;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class V240124 extends FrameLayout {

    private View mainView;
    private View slideView;

    private Scroller scroller;

    public V240124(@NonNull Context context) {
        super(context);

        scroller = new Scroller(context);
    }

    public V240124(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scroller = new Scroller(context);

    }

    public V240124(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scroller = new Scroller(context);
    }

    public V240124(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int childCount = getChildCount();
        if (childCount != 2) return;
        mainView = getChildAt(0);
        slideView = getChildAt(1);
        mainView.layout(0, 0, right, bottom);
        slideView.layout(-(right - left), 0, 0, bottom);
        width = getMeasuredWidth();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    int width;
    float downX = 0;
    float moveX = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                return true;
            case MotionEvent.ACTION_MOVE:
                if (slideView != null) {
                    float v = event.getX();
                    moveX = v - downX;
                    if (moveX < 0) return true;
                    scrollTo((int) -moveX, 0);
                }
                return true;
            case MotionEvent.ACTION_UP:
                if (moveX >= width / 2f) {
                    scrollTo(-width, 0);
                    moveX = width;
                } else {
                    scrollTo(0, 0);
                    moveX = 0;
                }
                super.performClick();
                return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
